package com.analysis.common;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class FileUtil {
	
	public static void write(String path,Object data) {
		write(Paths.get(path).toFile(), JSONObject.toJSONString(data));
	}
	
	public static void write(File file,Object data) {
		write(file, JSONObject.toJSONString(data));
	}
	
	public static void write(String path,String data) {
		write(Paths.get(path).toFile(), data);
	}
	
	public static void write(File file,String data) {
		 try {
				FileUtils.writeStringToFile(file, data, Charset.defaultCharset());
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	
    public static void writeLine(String path, Object data) {
		writeLine(Paths.get(path).toFile(), JSONObject.toJSONString(data));
	}
	
    public static void writeLine(File file, Object data) {
		writeLine(file, JSONObject.toJSONString(data));
	}

    public static void delete(String path) {
        FileUtils.deleteQuietly(Paths.get(path).toFile());
    }

    public static void delete(File file) {
        FileUtils.deleteQuietly(file);
    }
	
    public static void writeLine(String path, String data) {
		writeLine(Paths.get(path).toFile(), data);
	}
	
    public static void writeLine(File file, String data) {
		 try {
				FileUtils.writeLines(file, Charset.defaultCharset().name(), Arrays.asList(data), true);
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	public static String read(String path) {
		return read(Paths.get(path).toFile());
	}
	
	public static <T> T read(String path,Class<T> clazz) {
		String content = read(path);
		T obj = null;
		if(StringUtils.isNotEmpty(content)) {
            obj = JSONObject.parseObject(content, clazz);
   	    }
		return obj;
	}

    public static <T> List<T> readArray(String path, Class<T> clazz) {
        String content = read(path);
        List<T> obj = null;
        if (StringUtils.isNotEmpty(content)) {
            obj = JSON.parseArray(content, clazz);
        }
        return obj;
    }

	public static String read(File path) {
		String content = null;
		if (path.exists()) {
			try {
				content = FileUtils.readFileToString(path, Charset.defaultCharset());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return content;
	}

	public static String read(Path path) {
		return read(path.toFile());
	}
	
	public static List<String> readLines(String path) {
		return readLines(Paths.get(path).toFile());
	}
	
	public static List<String> readLines(File file){
		List<String> result = new ArrayList<>();
		if (file.exists()) {
			try {
				result = FileUtils.readLines(file, Charset.defaultCharset());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return result;
	}
}
